//
//  VC12Thread.swift
//  ChapterAll
//
//  Created by Kevin on 2020/5/6.
//  Copyright © 2020 Kevin. All rights reserved.
//

import UIKit

class VC12Thread: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        
    }
    
    var tickets:Int = 30
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        test10()
    }
    
    // 卖票
    func test10() -> Void {
        let thread1 = Thread(target: self, selector: #selector(saleMovies), object: nil)
        thread1.name = "thread1"
        
        let thread2 = Thread(target: self, selector: #selector(saleMovies), object: nil)
        thread2.name = "thread2"
        
        thread1.start()
        thread2.start()
    }
    
    @objc func saleMovies() -> Void {
        while true {
            // 同步锁
            objc_sync_enter(self)
            do {
                if tickets > 0 {
                    tickets -= 1
                    print("票剩余\(tickets)张，售票员:\(Thread.current)")
                }else {
                    print("没票了,\(Thread.current),\(tickets)")
                    break
                }
                objc_sync_exit(self)
            }
        }
    }
    
    
    // 卖票
    func test9() -> Void {
        let thread1 = Thread(target: self, selector: #selector(saleTicket), object: nil)
        thread1.name = "thread1"
        
        let thread2 = Thread(target: self, selector: #selector(saleTicket), object: nil)
        thread2.name = "thread2"
        
        thread1.start()
        thread2.start()
    }
    
    @objc func saleTicket() -> Void {
        while true {
            Thread.sleep(forTimeInterval: 1)
            // 同步锁
            objc_sync_enter(self)
            do {
                if tickets > 0 {
                    tickets -= 1
                    print("票剩余\(tickets)张，售票员:\(Thread.current)")
                }else {
                    print("没票了,\(Thread.current),\(tickets)")
                    break
                }
                objc_sync_exit(self)
            }
        }
    }
    
    // 线程退出
    func test8() -> Void {
        Thread.detachNewThread {
            self.threadExit()
        }
    }
    
    @objc func threadExit() -> Void {
        print("threadExit = \(Thread.current)")
        for i in 0..<5 {
            if i == 3 {
                Thread.exit()
            }
            print("\(Thread.current),\(i)")
        }
    }
    
    // 线程睡眠
    func test7() -> Void {
        let thread = Thread(target: self, selector: #selector(sleepThread), object: nil)
        thread.start()
    }
    
    @objc func sleepThread() -> Void {
        print("sleepThread = \(Thread.current)")
        Thread.sleep(until: Date(timeIntervalSinceNow: TimeInterval(integerLiteral: 5)))
        clickMethod()
    }
    
    // 线程优先级
    func test6() -> Void {
        let thread1 = Thread(target: self, selector: #selector(clickMethod), object: nil)
        thread1.name = "suma"
        thread1.threadPriority = 0.1
        thread1.start()
        
        let thread2 = Thread(target: self, selector: #selector(clickMethod), object: nil)
        thread2.name = "sumb"
        thread2.threadPriority = 1.0
        thread2.start()
    }
    
    // 线程属性
    func test5() -> Void {
        let thread = Thread(target: self, selector: #selector(clickMethod), object: nil)
        thread.name = "suma"
        thread.start()
    }
    
    // 子线程
    func test4() -> Void {
        Thread.detachNewThread {
            self.clickMethod()
        }
    }
    
    // 子线程
    func test3() -> Void {
        let thread = Thread(target: self, selector: #selector(clickMethod), object: nil)
        thread.start()
    }
    
    // 子线程
    func test2() -> Void {
        performSelector(inBackground: #selector(clickMethod), with: nil)
    }
    
    // 主线程
    func test1() -> Void {
        print("\(Thread.current)")
    }
    
    @objc func clickMethod() -> Void {
        for i in 0..<5 {
            print("\(Thread.current),\(i)")
        }
    }
}
